Introduction Avancée

Mickaël Canouil, Ph.D.

Dernière mise à jour : 2022-05-21

Introduction à

Installation de

Site internet officiel : https://www.r-project.org/

R is a language and environment for statistical computing and graphics. It is a GNU project which is similar to the S language and environment which was developed at Bell Laboratories (formerly AT&T, now Lucent Technologies) by John Chambers and colleagues. R can be considered as a different implementation of S. There are some important differences, but much code written for S runs unaltered under R.

R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, …) and graphical techniques, and is highly extensible. The S language is often the vehicle of choice for research in statistical methodology, and R provides an Open Source route to participation in that activity.

One of R’s strengths is the ease with which well-designed publication-quality plots can be produced, including mathematical symbols and formulae where needed. Great care has been taken over the defaults for the minor design choices in graphics, but the user retains full control.

Download R

Sauvegarder le code …

Pas l’environnement !

Les scripts :

  • standards”: .R

  • Rmarkdown”:.Rmd

Les “données” :

  • objet”: .rds

  • liste d’objet”: .Rdata

Utiliser un IDE1

Faire table rase à chaque démarrage !

Lorsque vous quittez  :

  • Ne pas enregistrer votre espace de travail (workspace1) !

Lorsque vous démarrez  :

  • Ne pas charger l’espace de travail sauvegardé précédemment !

Faire table rase !

Dans Windows

Faire table rase !

Dans RStudio (Tools > Global options)

Faire table rase !

  • Dans un Terminal
R --no-save --no-restore-data
  • .bash_profile / .bashrc / …
alias R='R --no-save --no-restore-data'

Comment Réinitialiser  ?

Avec rm(list = ls()) ?

If the first line of your R script is

rm(list = ls())

I will come into your office and SET YOUR COMPUTER ON FIRE 🔥.

— Jenny Bryan

Que ne fait pas rm(list = ls()) ?

  • Réinitialiser la session en cours

  • Réinitialiser les options()options(stringsAsFactors = FALSE)1

  • Réinitialiser le répertoire de travail getwd()setwd()

  • Réinitialiser les extensions attachées

Alors que fait rm(list = ls()) ?

help(rm)

Réinitialiser

  • Dans une console

    • Via le raccourci Ctrl+D

    • Via la fonction q() dans une console

Réinitialiser

  • Dans RStudio :

    • Via le raccourci Ctrl+Shift+F10

    • Via le menu

Désactiver les fichiers de démarrage

  • --vanilla,
    pour désactiver le chargement de tous les fichiers de démarrage

  • --no-init-file,
    pour désactiver le chargement du fichier .Rprofile

  • --no-environ,
    pour désactiver le chargement du fichier .Renviron

setwd(...) en première ligne …

If the first line of your R script is

setwd("C:\Users\jenny\path\that\only\I\have")

I will come into your office and SET YOUR COMPUTER ON FIRE 🔥.

— Jenny Bryan

Faisons le Point sur setwd() !

library(ggplot2)

setwd("/path/to/a/directory/on/my/laptop/data")

df <- read.delim("data.csv")

p <- ggplot(df, aes(x, y)) + geom_point()

ggsave("../figs/scatterplot.png")


library(ggplot2)

setwd("/path/to/a/directory/on/my/laptop/data")

df <- read.delim("data.csv")

p <- ggplot(df, aes(x, y)) + geom_point()

setwd("/path/to/a/directory/on/my/laptop/figs")

ggsave("scatterplot.png")

L’extension here

here::here()
here::i_am("radvanced.qmd")
here::i_am("content/content-01.qmd")

Utiliser un Mode “Projet” via un IDE

  • Démarrer un projet A :

    1. Démarre R.

    2. Défini le répetoire de travail de R comme la racine du projet A.

  • Passer d’un projet A à un project B :

    1. Redémarre R.

    2. Défini le répetoire de travail de R comme la racine du projet B.

Utiliser un Mode “Projet”

Lectures Utiles